package com.arm.controller.regedit;


import com.arm.controller.WechatBaseController;
import com.arm.def.Constant;
import com.arm.module.User;
import com.arm.module.dto.Regedit;
import com.arm.module.weChat.UserWechatInfo;
import com.arm.service.RegeditService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Baggio on 2018-6-22.
 */
@RestController
@RequestMapping("/regedit")
public class RegeditController extends WechatBaseController {

    @Autowired
    RegeditService regeditService;

    /**
     * 获取验证码
     */
    @GetMapping(value = "/{phone}")
    public ResultObj getVerificationCode(@PathVariable("phone") String phone) {


        ResultObj resultObj = new ResultObj();
        try {
            String key = Constant.SESSION_KEY_SMS_RANDOM_CODE;
            String code = regeditService.getCode(phone);
            super.session.setAttribute(key, code);
            logger.info("随机验证码：" + code);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }

    @PostMapping(value = "/regedit")
    public ResultObj regedit(@RequestBody Regedit regedit) {
        ResultObj resultObj = new ResultObj();

        try {
            String key = Constant.SESSION_KEY_SMS_RANDOM_CODE;
            String code = (String) super.session.getAttribute(key);
            if (StringUtils.isBlank(code)) {
                throw new MyRunTimeExcption(ReturnCode.SMS_CODE_IS_NULL);
            }
            if (!code.equals(regedit.getCode())) {
                throw new MyRunTimeExcption(ReturnCode.SMS_CODE_IS_ERRO);
            }
            String openId = (String) super.session.getAttribute(Constant.SESSION_OPEN_ID);
            UserWechatInfo userWechatInfo = (UserWechatInfo) super.session.getAttribute(Constant.SESSION_WECHAT_USER_INFO);
            regedit.setOpenId(openId);
            regedit.setPhoto(userWechatInfo.getHeadimgurl());

            User user = regeditService.regedit(regedit);
            super.session.setAttribute(Constant.SESSION_KEY_USER_LOGIN, user);
            String toUrl = (String) session.getAttribute(Constant.THIS_URL);
            if (StringUtils.isBlank(toUrl)) {
                toUrl = "/weChat/myAMC/index";
            }
            resultObj.setData(toUrl);
            regeditService.regedit(regedit);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }


    @RequestMapping(value = "/gotoRegedit")
    public ModelAndView gotoLogin() {
        ModelAndView mov = new ModelAndView();
        if (super.getIsMobile(super.httpServletRequest)) {
            super.oauth2(super.httpServletRequest, super.httpServletResponse);
        }
        mov.setViewName("weChat/regedit/regedit");
        return mov;
    }


}
